<?php

namespace App\Http\Repositories;

use App\Traits\SpliceQuery;
use Illuminate\Support\Facades\Auth;

class Repository
{
    use SpliceQuery;

    const MODEL = null;

    /**
     * @return mixed
     */
    public function query()
    {
        return call_user_func(static::MODEL . '::query');
    }

    public function model()
    {
        return call_user_func(static::MODEL . '::newModel');
    }

    /**
     * 根据条件查询唯一（组合唯一）值是否已经存在
     * 存在(且id不等于传入id)返回true
     */
    public function uniqueExist(array $conditions, int $id = 0)
    {
        $query = $this->query();
        foreach ($conditions as $field => $value) {
            $query->where($field, $value);
        }
        $query = $query->first();

        if (!empty($query) && ($id != $query->id)) return true;

        return false;
    }

    public function getByConditions($conditions = [], $select_fields = '*')
    {
        return $this->query()->select($select_fields)->where($conditions)->get();
    }
    public function getOneByConditions(array $conditions = [])
    {
        return $this->query()->where($conditions)->first();
    }

    public function getById($id)
    {
        return $this->query()->where('id', $id)->first();
    }

    public function getByIds(array $ids)
    {
        return $this->query()->whereIn('id', $ids)->get();
    }

    protected function returnArray($query)
    {
        if (!is_array($query)) {
            return !empty($query->get()) ? $query->get()->toArray() : [];
        }
        return $query;
    }

    /**
     * 创建一条数据
     */
    public function createOne(array $params)
    {
        $args = $params;
        $now = date('Y-m-d H:i:s');
        if (empty($params['created_at'])) {
            $args = array_merge(['created_at' => $now], $args);
        }
        $args = array_merge(['updated_at' => $now], $args);
        $id = $this->query()->insertGetId($args);

        // return $this->returnArray($this->query()->where('id', $id)->first());
        return $this->query()->where('id', $id)->first();
    }
    /**
     * 创建多条数据
     */
    public function createMany(array $params)
    {
        $args = $params;
        $now = date('Y-m-d H:i:s');
        foreach ($args as &$arg) {
            if (empty($arg['created_at'])) {
                $arg = array_merge(['created_at' => $now], $arg);
            }
            $arg = array_merge(['updated_at' => $now], $arg);
        }
        return $this->query()->insert($args);
    }

    public function deleteById(int $id)
    {
        return $this->query()->where('id', $id)->delete();
    }
    public function deleteByConditions(array $conditions)
    {
        if (empty($conditions)) abort(422, 'not null');
        $query = $this->query();
        foreach ($conditions as $field => $value) {
            $query->where($field, $value);
        }

        return $query->delete();
    }

    /**
     * 根据id修改数据
     */
    public function editById(int $id, array $params)
    {
        $now = date('Y-m-d H:i:s', time());
        $args = array_merge(['updated_at' => $now], $params);
        $element = $this->query()->where('id', $id)->update($args);

        return $this->returnArray($this->query()->where('id', $id));
    }
    /**
     * 根据条件修改数据
     */
    public function editByConditions($conditions, array $attributes)
    {
    }

    /**
     * 固定格式查询结果
     */
    public function formatSearch(array $conditions, $paginate = true)
    {
        $query = $this->query();

        $this->spliceQueryConditions($query, $conditions);

        return $this->paginate($query, $paginate);
    }
    public function paginate($query, $paginate = true, $page_num = 10)
    {
        if ($paginate) {
            return $query->paginate($page_num);
        } else {
            return $query->get();
        }
    }

    /**
     * 某字段加一
     */
    public function fieldAddOne($conditions, $field)
    {
        $this->query()->where($conditions)->increment($field);
    }
}
